package org.jeecg.modules.inspection.controller;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.inspection.entity.Accept;
import org.jeecg.modules.inspection.entity.InspectionList;

import org.jeecg.modules.inspection.service.IInspectionDataApiService;
import org.jeecg.modules.inspection.service.IPinspectionItemService;
import org.jeecg.modules.inspection.util.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@Slf4j
@Api(tags="巡检Api接口")
@RestController
public class InspectionDataApiController {

    @Autowired
    private IInspectionDataApiService inspectionDataApiService;
    @Autowired
    private RestUtil restUtil;

    @Autowired
    private IPinspectionItemService inspectionItemService;

    private static final String  tokenUrl ="http://www.962121.fgj.sh.gov.cn/hmfmstest/sharingplatform/sso/oauth/token?username=上海永绿置业有限公司第三分公司&password=111111a&grant_type=password&client_id=c1&client_secret=secret&scope=read";

    private static final String  url ="http://www.962121.fgj.sh.gov.cn/hmfmstest/sharingplatform/api/v1/databack/common/specificDataBack?specId=322144773494939648";


    /**
     *   生成巡检任务
     * @return
     */
    @RequestMapping(value = "/api/ins/generateInsTask")
    public void generateInsTask() {
        inspectionDataApiService.generateInsTask();
    }

    /**
     *   生成巡检任务
     * @return
     */
    @RequestMapping(value = "/b/api/ins/generateInsTask")
    public void generateInsTaskB() {
        inspectionDataApiService.generateInsTask();
    }

    @RequestMapping("/api/ins/getExceptionTask")
    public Result<List<InspectionList>> getAllInspectionException(){
        Result<List<InspectionList>> result = new Result<>();
        List<Accept> accept = inspectionItemService.selectExceptionItemByProjectId();
        if (accept.size() > 0){
        JSONArray jsonArray = JSONArray.fromObject(accept);
         /* result.setResult(inspectionList);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        Accept accept = new Accept();
        objectObjectHashMap.put("sectId","120302190615347");
        objectObjectHashMap.put("otherSectId","1");
        objectObjectHashMap.put("inspectionList",inspectionList);*/
        JSONObject jsonObject = restUtil.postForEntity(jsonArray,url,getAccessToken(), null);
        String code = (String) jsonObject.get("code");
        if (code.equals("200")){
            log.error("推送成功");
        }
        }
        return null;
    }

    public String getAccessToken(){
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("username","上海永绿置业有限公司第三分公司");
        objectObjectHashMap.put("password","111111a");
        objectObjectHashMap.put("grant_type","password");
        objectObjectHashMap.put("client_id","c1");
        objectObjectHashMap.put("client_secret","secret");
        objectObjectHashMap.put("scope","read");
        JSONObject jsonObject = restUtil.postForEntity(null, tokenUrl,null);
        String access_token = (String) jsonObject.get("access_token");
        return access_token;
    }
}
